package com.sanpu.kxe.business.trade.view;

import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.RebateTotalOutput;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.trade.adapter.MyRebTotalAdapter;
import com.sanpu.kxe.business.trade.bean.TabEntity;
import com.sanpu.kxe.business.trade.contract.TradeConfig;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.view.ScrollableFragment;
import com.sanpu.kxe.module.immersionbar.ImmersionBar;
import com.sanpu.kxe.module.scrollable.CanScrollVerticallyDelegate;
import com.sanpu.kxe.module.scrollable.ScrollableLayout;
import com.sanpu.kxe.module.tablelayout.CommonTabLayout;
import com.sanpu.kxe.module.tablelayout.CustomTabEntity;
import com.sanpu.kxe.module.tablelayout.listener.OnTabSelectListener;
import com.sanpu.kxe.module.widget.WaveHelper;
import com.sanpu.kxe.module.widget.WaveView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Description：我的返佣
 * Created by 薛勇军 on 2018/8/6.
 */

public class MyRebTotalActivity extends BaseTitleActivity {

    @Bind(R.id.count_reb)
    TextView vCountReb;
    @Bind(R.id.month_reb)
    TextView vMonthReb;
    @Bind(R.id.scrollable_layout)
    ScrollableLayout vScrollableLayout;
    @Bind(R.id.view_page)
    ViewPager vViewPage;

    @Bind(R.id.wave)
    WaveView vWave;
    @Bind(R.id.tab_layout)
    CommonTabLayout vTabLayout;
    @Bind(R.id.navigation)
    LinearLayout vNavigation;

    private MyRebTotalAdapter mRebTotalAdapter;

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_rebtotal;
    }

    @Override
    protected void initView() {
        super.initView();
        mTabEntities.add(new TabEntity("三级内返佣", 0, 0));
        mTabEntities.add(new TabEntity("三级外返佣", 0, 0));
        vTabLayout.setTabData(mTabEntities);
        int mCheckPos = getIntent().getIntExtra(TradeConfig.INTENT_CURRENT_ITEM, 0);
        boolean isPartner = false;
        UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput != null && centerOutput.isPartner()) {
            isPartner = centerOutput.isPartner();
//            vTabLayout.setVisibility(View.VISIBLE);
//            vNavigation.setVisibility(View.GONE);
            vTabLayout.setVisibility(View.GONE);
            vNavigation.setVisibility(View.VISIBLE);
        } else {
            vTabLayout.setVisibility(View.GONE);
            vNavigation.setVisibility(View.VISIBLE);
        }
        mRebTotalAdapter = new MyRebTotalAdapter(getSupportFragmentManager(), isPartner);
        vScrollableLayout.setDraggableView(vNavigation);
        vScrollableLayout.setCanScrollVerticallyDelegate(new CanScrollVerticallyDelegate() {
            @Override
            public boolean canScrollVertically(int direction) {
                final ScrollableFragment fragment = mRebTotalAdapter.getItem(vViewPage.getCurrentItem());
                return fragment != null && fragment.canScrollVertically(direction);
            }
        });

        vViewPage.setOffscreenPageLimit(2);
        vViewPage.setAdapter(mRebTotalAdapter);
        vViewPage.setCurrentItem(mCheckPos);
        vTabLayout.setCurrentTab(mCheckPos);

        WaveHelper waveHelper = new WaveHelper(vWave);
        vWave.setShapeType(WaveView.ShapeType.SQUARE);
        waveHelper.start();
    }

    @Override
    protected void initData() {
        super.initData();
        RebateTotalOutput totalOutput = CacheManage.getInstance().getCache(CacheModel.REBATETOTAL);
        if (totalOutput != null) {
            if (!TextUtils.isEmpty(totalOutput.getMonRebAmt())) {
                vMonthReb.setText(totalOutput.getMonRebAmt());
            }
            if (totalOutput.getAccRebAmt() != null) {
                vCountReb.setText(totalOutput.getAccRebAmt().toPlainString());
            }
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        ButterKnife.findById(this, R.id.leftIv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        vTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vViewPage.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        vViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                vTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    protected boolean hasCommonTitle() {
        return false;
    }

    @Override
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColorTransformEnable(true)
                .statusBarView(R.id.top_view)
                .statusBarColor(R.color.color_base)
                .keyboardEnable(true)
                .navigationBarWithKitkatEnable(false)
                .init();
    }
}
